<template>
  <div id="apptb">
    <div class="grouptitle">
      <div class="grouptitletext">人员信息管理</div>
    </div>

    <div class="contentall">
      <div class="contentallcontent">
        <div class="contentss">
          <div class="inputall">
            <div class="inputleft">
              <label for="">身份</label>
              <select
                id="continent"
                class="inputtextte"
                v-model="staffInfo.statusname"
              >
                <option value="领队">领队</option>
                <option value="教练">教练</option>
                <option value="裁判">裁判</option>
                <option value="运动员">运动员</option>
                <option value="陪练人员">陪练人员</option>
                <option value="随队人员或队医">随队人员或队医</option>
              </select>
            </div>
            <div class="inputright">
              <label for="">参赛组别</label>
              <select
                id="continent"
                class="inputtextte"
                v-model="staffInfo.csgroup"
              >
                <option value="无">无</option>
                <option value="组别1">组别1</option>
                <option value="组别2">组别2</option>
                <option value="组别3">组别3</option>
              </select>
            </div>
          </div>
          <!--  -->
          <div class="inputall">
            <div class="inputleft">
              <label for="">姓名</label>
              <input
                type="text"
                class="inputtextte"
                v-model="staffInfo.username"
              />
            </div>
            <div class="inputright">
              <label for="">身份证号码</label>
              <input
                type="text"
                class="inputtextte"
                v-model="staffInfo.idcard"
              />
            </div>
          </div>
          <div class="inputall">
            <div class="inputleft">
              <label for="">性别</label>
              <select
                id="continent"
                class="inputtextte"
                v-model="staffInfo.sex"
              >
                <option value="男">男</option>
                <option value="女">女</option>
              </select>
            </div>
            <div class="inputright">
              <label for="">出生日期</label>
              <input
                type="text"
                class="inputtextte"
                v-model="staffInfo.birthday"
              />
            </div>
          </div>
          <div class="inputall">
            <div class="inputleft">
              <label for="">手机号码</label>
              <input
                type="text"
                class="inputtextte"
                v-model="staffInfo.phone"
              />
            </div>
            <div class="inputright">
              <label for="">邮箱</label>
              <input
                type="text"
                class="inputtextte"
                v-model="staffInfo.email"
              />
            </div>
          </div>

          <div class="inputall" id="ipumew">
            <div class="bottomimg" @click="submitshut">
              <i class="el-icon-circle-plus-outline iconsel"></i>
              <span>保存</span>
            </div>
            <div class="bottomimg" style="margin-left: 15px" @click="submitgo">
              <i class="el-icon-right iconsel"></i>
              <span>下一步</span>
            </div>
          </div>
        </div>

        <div class="bottombox"></div>
      </div>
    </div>
  </div>
</template>

<script>
import axios from "axios";

export default {
  data: function () {
    return {
      checkList: [
        { id: 1, name: "综合服务" },
        { id: 2, name: "参加晚宴" },
        { id: 3, name: "未成年人拼床" },
        { id: 4, name: "单人间" },
      ],
      isVip: false,

      staffInfo: {
        statusname: "",
        csgroup: "",
        username: "",
        idcard: "",
        sex: "",
        birthday: "",
        phone: "",
        email: "",
      },
      errorMessages: [], // 用于收集错误消息的数组
      isupdateAdd: false,
      updateId: 0,
    };
  },

  methods: {
    // 验证：邮箱、电话号码
    validateTeamInfo() {
      // 验证所有字段是否非空
      if (
        this.staffInfo.statusname == "" ||
        this.staffInfo.csgroup == "" ||
        this.staffInfo.username == "" ||
        this.staffInfo.idcard == "" ||
        this.staffInfo.sex == "" ||
        this.staffInfo.birthday == "" ||
        this.staffInfo.phone == "" ||
        this.staffInfo.email == ""
      ) {
        this.$message({
          message: "请将信息填写完整",
          type: "error",
        });
        return;
      }

      // 验证手机号码是否为11位纯数字
      const isValidPhone = /^\d{11}$/.test(this.staffInfo.phone.trim());
      if (!isValidPhone) {
        this.$message({
          message: "手机号需11位数字",
          type: "error",
        });
        return;
      }

      // 验证电子邮件是否包含@符号
      const isValidEmail = this.staffInfo.email.trim().includes("@");
      if (!isValidEmail) {
        this.$message({
          message: "E-Mall请填写正确",
          type: "error",
        });
        return;
      }

      return true;
    },

    // 保存并关闭：
    submitshut() {
      if (!this.validateTeamInfo()) return;

      if (this.isupdateAdd) {
        this.updategetbyid(this.updateId);
        this.$message({
          message: "修改成功",
          type: "success",
        });
        setTimeout(() => {
          window.close();
        }, 1000);
      } else {
        this.addstaff();
        this.$message({
          message: "添加成功",
          type: "success",
        });
        setTimeout(() => {
          window.close();
        }, 1000);
        localStorage.setItem("updateOne", JSON.stringify(true));
      }
    },

    // 保存并继续：
    submitgo() {
      this.submitshut();
    },

    // 新增 人员：
    addstaff() {
      axios({
        method: "post",
        url: "http://localhost:3002/competition/staff/add",
        data: this.staffInfo,
      }).then((res) => {
        // console.log(res.data);
      });
    },

    // 修改 人员
    updategetbyid(id) {
      axios({
        method: "post",
        url: "http://localhost:3002/competition/staff/update",
        data: {
          id: id,
          statusname: this.staffInfo.statusname,
          job: this.staffInfo.job,
          idcard: this.staffInfo.idcard,
          username: this.staffInfo.username,
          sex: this.staffInfo.sex,
          birthday: this.staffInfo.birthday,
          phone: this.staffInfo.phone,
          email: this.staffInfo.email,
          vipdata: this.staffInfo.vipdata,
          danxz: this.staffInfo.danxz,
          iddan: this.staffInfo.iddan,
          service: this.staffInfo.service,
          danbk: this.staffInfo.danbk,
          association: this.staffInfo.association,
          jiazhu: this.staffInfo.jiazhu,
          tour: this.staffInfo.tour,
          souvenir: this.staffInfo.souvenir,
          censusr: this.staffInfo.censusr,
          img: this.staffInfo.img,
        },
      }).then((res) => {
        // console.log(res.data);
        // localStorage.setItem("updateOne", JSON.stringify(true));
      });
    },

    // 获取 单个人员信息
    getstaffone() {
      axios({
        url: "http://localhost:3002/competition/staff/getbyId",
        method: "post",
        data: { id: this.updateId },
      }).then((res) => {
        // console.log(res.data.list[0]);
        this.staffInfo = res.data.list[0];
      });
    },

    getservice() {
      console.log(this.staffInfo.service);
    },

    getjob(e) {},

    submitall() {},
  },

  mounted() {
    // 获取 修改 按钮 传递 过来的id：
    var url = window.location.href;
    var params = new URLSearchParams(url.split("?")[1]);
    var id = params.get("id");
    this.updateId = id;
    // console.log(this.updateId); // 打印出 id 参数的值
    if (id) {
      this.getstaffone();
      this.isupdateAdd = true;
    }
  },
};
</script>

<style lang="less" scoped>
.iconsel {
  margin-right: 10px;
}
#ipumew {
  display: flex;
  justify-content: flex-end;

  .bottomimg {
    margin-right: 20px !important;
  }
}
#apptb {
  margin: auto;
  margin-top: 40px;
  background-color: #7c7e7c;
  width: 60%;
  height: 85%;
  .grouptitle {
    width: 100%;
    height: 45px;
    background-color: #7c7e7c;
    color: #fff;
    display: flex;
    justify-content: center; /* 水平居中 */
    align-items: center; /* 垂直居中 */
  }
  .contentall {
    background-color: #7c7e7c;

    width: 100%;
    height: 92%;
    padding-top: 6px;
    .contentallcontent {
      width: 98.5%;
      height: 99%;
      background-color: #edefe9;
      margin: 0 6px;
      border-radius: 10px;
      position: relative;
      .contentss {
        width: 98%;
        margin: 0 10px;
        background-color: #f4f6f8;
        transform: translateY(3%);
        border: 1px solid rgb(206, 200, 200);
        // 每一行
        .inputall {
          background-color: #f4f6f8;

          margin: 0px 6px;
          height: 40px;
          font-size: 15px;
          border-bottom: 1px solid rgb(206, 200, 200);
          display: flex;
          align-items: center; /* 垂直居中 */
          select {
            height: 25px;
            width: 227px !important;
          }
          .bottomimg {
            display: flex;
            align-items: center; /* 垂直居中 */
            background: linear-gradient(#f6f6f6, #e1e1e1);
            padding: 4px 10px;
            border-radius: 5px;
            border: 1px solid rgb(174, 171, 171);
            img {
              width: 20px;
              height: 20px;
              margin-right: 8px;
            }
          }

          .inputleft {
            display: inline-block;
            vertical-align: top;
            padding: 0 6px;
            .xqdzipt {
              display: inline-block;
              width: 400px !important;
            }
            label {
              display: inline-block;
              width: 80px;
              text-align: center;
            }
            .inputtextte {
              width: 220px;
            }
          }
          .inputright {
            display: inline-block;
            vertical-align: top;
            margin-left: 5px;
            label {
              display: inline-block;
              width: 80px;
              text-align: center;
            }
            .inputtextte {
              width: 220px;
            }
          }
        }
      }

      .bottombox {
        background-color: #eff2f5;
        width: 100%;
        height: 18px;
        position: absolute;
        bottom: 0;
      }
    }
  }
}
</style>